package com.wthink.newMain.settings.config.inner.reboot

import android.app.Activity
import android.app.TimePickerDialog
import android.app.TimePickerDialog.OnTimeSetListener
import android.content.Context
import android.os.Bundle
import android.os.IBinder
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.TextView
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import com.wthink.hzgj.R
import com.wthink.hzgj.databinding.FragmentRebootBinding
import com.wthink.hzgj.util.toast
import kotlinx.android.synthetic.main.fragment_reboot.*
import java.lang.reflect.Method
import java.util.*


class RebootFragment : Fragment(){
    private val viewModel by viewModels<RebootViewmodel>()

    private lateinit var binding: FragmentRebootBinding

    var calendar: Calendar? = null

    var type = ""
    var backLightTime = ""

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        binding = DataBindingUtil.inflate(
            inflater,
            R.layout.fragment_reboot,
            container,
            false
        )
        binding.viewModel = viewModel
        binding.lifecycleOwner = this
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)
        calendar = Calendar.getInstance(Locale.CHINA)
        viewModel.start {
            activity?.runOnUiThread {
                it.type_list.forEachIndexed { index, s ->
                    if (s == it.type) {
                        binding.spinnerFire.setSelection(index)
                    }
                }

                binding.editDelay.setText(it.delay_time)
                binding.startTime.text = it.start_time
                binding.endTime.text = it.end_time
            }
        }

        binding.spinnerFire.onItemSelectedListener =  object : AdapterView.OnItemSelectedListener {
            override fun onNothingSelected(parent: AdapterView<*>?) {

            }
            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                // viewModel.baudRate1.postValue(viewModel.baudrateList.value!![position])
                type = viewModel.fireData.value!!.type_list[position]
            }
        }

        binding.spinnerBackLight.onItemSelectedListener =  object : AdapterView.OnItemSelectedListener {
            override fun onNothingSelected(parent: AdapterView<*>?) {

            }
            override fun onItemSelected(parent: AdapterView<*>?, view: View?, position: Int, id: Long) {
                backLightTime = viewModel.fireData.value!!.back_light_time_list[position]
            }
        }

        binding.commit.setOnClickListener {
            val delayTime = edit_delay.text.toString()
            val startTime = startTime.text.toString()
            val endTime = endTime.text.toString()
            if (delayTime.isEmpty() || startTime.isEmpty() || endTime.isEmpty() || backLightTime.isEmpty() || type.isEmpty()) {
                toast(activity!!,"请输入完整信息")
                return@setOnClickListener
            }
            viewModel.config(type,delayTime,startTime,endTime,backLightTime)
        }

        binding.startTime.setOnClickListener {
            selectStartTime(activity!!,4,startTime,calendar!!)
        }

        binding.endTime.setOnClickListener {
            selectEndTime(activity!!,4,endTime,calendar!!)
        }

        viewModel.getTime {
            activity?.runOnUiThread {
                val date = Date(it.toLong())
                calendar?.time = date
            }
        }
    }

    fun selectStartTime(
        activity: Activity?,
        themeResId: Int,
        textView: TextView,
        calendar: Calendar
    ) {
        TimePickerDialog(activity, themeResId,
            OnTimeSetListener { view, hourOfDay, minute ->
                if (hourOfDay >= 10) {
                    if (minute >= 10) {
                        textView.text = "$hourOfDay:$minute"
                    } else {
                        textView.text = "$hourOfDay:0$minute"
                    }
                } else {
                    if (minute >= 10) {
                        textView.text = "0$hourOfDay:$minute"
                    } else {
                        textView.text = "0$hourOfDay:0$minute"
                    }
                }
            }
            , calendar.get(Calendar.HOUR_OF_DAY)
            , calendar.get(Calendar.MINUTE)
            , true).show()
    }

    fun selectEndTime(
    activity: Activity?,
        themeResId: Int,
        textView: TextView,
        calendar: Calendar
    ) {
        TimePickerDialog(activity, themeResId,
            OnTimeSetListener { view, hourOfDay, minute ->
                if (hourOfDay >= 10) {
                    if (minute >= 10) {
                        textView.text = "$hourOfDay:$minute"
                    } else {
                        textView.text = "$hourOfDay:0$minute"
                    }
                } else {
                    if (minute >= 10) {
                        textView.text = "0$hourOfDay:$minute"
                    } else {
                        textView.text = "0$hourOfDay:0$minute"
                    }
                }
            }
            , calendar.get(Calendar.HOUR_OF_DAY)
            , calendar.get(Calendar.MINUTE)
            , true).show()
    }
}